/**
 * @project_name staff_vehicle
 * @package_name com.ssh.dao
 * @desc TODO
 * @param
 * @author jack
 * @date 2013-4-25
 * @time 上午7:03:39
 */
package com.ssh.dao;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.ssh.entity.FcarAplly;
import com.ssh.model.pdata.FInOutApplyMPData;

/**
 * @author jack
 *
 */
public class FcarApllyDAOImpl implements FcarApllyDAOI {
	private SessionFactory sessionFactory;

	/*
	 * (non-Javadoc)
	 *
	 * @see com.ssh.dao.FcarApllyDAOI#addFcarAplly(com.ssh.entity.FcarAplly)
	 */
	@Override
	public FcarAplly addFcarAplly(FcarAplly fcarAplly) {
		// TODO Auto-generated method stub
		try {
			sessionFactory.getCurrentSession().save(fcarAplly);
		} catch (Exception e) {
			return null;
		}
		return fcarAplly;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see com.ssh.dao.FcarApllyDAOI#addFcarApllyList(java.util.List)
	 */
	@Override
	public int addFcarApllyList(List<FcarAplly> fcarApllyList) {
		// TODO Auto-generated method stub
		return 0;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see com.ssh.dao.FcarApllyDAOI#countById(java.lang.String)
	 */
	@Override
	public int countById(String id) {
		// TODO Auto-generated method stub
		return 0;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see com.ssh.dao.FcarApllyDAOI#deleteById(java.lang.String)
	 */
	@Override
	public int deleteById(String id) {
		// TODO Auto-generated method stub
		int num;
		try {
			String hql = "delete FcarAplly as f where f.fcarCarid=?";
			num = sessionFactory.getCurrentSession().createQuery(hql)
					.setString(0, hql).executeUpdate();
		} catch (Exception e) {
			return 0;
		}
		return num;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see com.ssh.dao.FcarApllyDAOI#deleteByobject(com.ssh.entity.FcarAplly)
	 */
	@Override
	public int deleteByobject(FcarAplly fcarAplly) {
		// TODO Auto-generated method stub
		try {
			sessionFactory.getCurrentSession().delete(fcarAplly);
		} catch (Exception e) {
			return 0;
		}
		return 1;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see com.ssh.dao.FcarApllyDAOI#getAllFInOutApply()
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<FInOutApplyMPData> getAllFInOutApply( ) {
		// TODO Auto-generated method stub
        /* private FcarAplly fcarAplly ;
		private FcarCard fcarCard ;
		private FcarDriver fcarDriver ;
		private Vehicle vehicle ;


		*/
		Session s=sessionFactory.getCurrentSession();
		String hql = "select new com.ssh.model.pdata.FInOutApplyMPData(fa,fd,v) " +
				"from FcarDriver as fd,Vehicle as v,FcarAplly as fa " +
				"where (fa.fcarCarid=v.vehicleId)  and (fd.DLicence.dlicenceId=fa.DLicence.dlicenceId)";
		List<FInOutApplyMPData> fInOutApplyMPDataList = s.createQuery(hql).list();
		return fInOutApplyMPDataList;
		/*Session s=sessionFactory.getCurrentSession();
		String hql=" from FcarAplly as s";
		List<FcarAplly> fcarApllys=s.createQuery(hql).list();
		Iterator<FcarAplly> itfca=fcarApllys.iterator();
		while (itfca.hasNext()){
			FcarAplly fa = itfca.next();
			fa.getDLicence().getDlicenceId();
		}

		hql=" from FcarCard as s";
		List<FcarCard> fcarCards=s.createQuery(hql).list();

		hql=" from FcarDriver as s";
		List<FcarDriver> fcarDrivers=s.createQuery(hql).list();
		hql=" from Vehicle as s";
		List<Vehicle> vehicles=s.createQuery(hql).list();
		Iterator<FcarAplly> itfa=fcarApllys.iterator();
		Iterator<FcarCard> itfc=fcarCards.iterator();
		Iterator<FcarDriver> itfd=fcarDrivers.iterator();
		Iterator<Vehicle> itv=vehicles.iterator();
		List<FInOutApplyMPData> list=new ArrayList<FInOutApplyMPData>();
		while (itfa.hasNext()){
			list.add(new FInOutApplyMPData(itfa.next(),itfc.next(),itfd.next(),itv.next()));
		}
		return list;*/
	}

	/**
	 * @return the sessionFactory
	 */
	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see com.ssh.dao.FcarApllyDAOI#queryBackList(java.lang.String)
	 */
	@Override
	public List<FcarAplly> queryBackList(String strings) {
		// TODO Auto-generated method s tub
		return null;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see com.ssh.dao.FcarApllyDAOI#queryById(java.lang.String)
	 */
	@Override
	public FcarAplly queryById(String id) {
		// TODO Auto-generated method stub
		FcarAplly fcarAplly;
		try {
			String hql = "from FcarAplly as f where f.fcarCarid=? ";
			fcarAplly = (FcarAplly) sessionFactory.getCurrentSession()
					.createQuery(hql).setString(0, id).uniqueResult();
		} catch (Exception e) {
			return null;
		}

		return fcarAplly;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see com.ssh.dao.FcarApllyDAOI#queryFInOutApply(java.lang.String)
	 */
	@Override
	public List<FInOutApplyMPData> queryFInOutApply(String driverId) {
		// TODO Auto-generated method stub
		String hql = " select new FInOutApplyMPData(fca,fd,v) from FcarAplly as fca, FcarDriver as fd ,Vehicle as v"
				+ "where fca.fcarCarid=v.vehicleId and fd=:driverId";
		@SuppressWarnings("unchecked")
		List<FInOutApplyMPData> fioalist = sessionFactory.getCurrentSession()
				.createQuery(hql).setParameter("driverId", driverId).list();
		return fioalist;
	}

	/**
	 * @param sessionFactory
	 *            the sessionFactory to set
	 */
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see com.ssh.dao.FcarApllyDAOI#updateByObject(com.ssh.entity.FcarAplly)
	 */
	@Override
	public FcarAplly updateByObject(FcarAplly fcarAplly) {
		// TODO Auto-generated method stub
		try {
			sessionFactory.getCurrentSession().update(fcarAplly);
		} catch (Exception e) {
			return null;
		}

		return fcarAplly;
	}

	/* (non-Javadoc)
	 * @see com.ssh.dao.FcarApllyDAOI#getFVehicleInVData()
	 */
	@Override
	public FInOutApplyMPData getFVehicleInVData(String driverId) {
		// TODO Auto-generated method stub
		String hql="select new com.ssh.model.pdata.FInOutApplyMPData(f,v) " +
				"from Vehicle as v,FcarDriver as f,FcarAplly as fc" +
				" where v.vehicleId=f.carId  and fc.fcarCarid=f.carId and f.driverId=:driverId " +
				"and fc.applyResult=:applyResult";
		FInOutApplyMPData fInOutApplyMPData = (FInOutApplyMPData) sessionFactory.getCurrentSession().
				createQuery(hql).setParameter("driverId", driverId).setInteger("applyResult", 2).uniqueResult();
		return fInOutApplyMPData;
	}

	@Override
	public FInOutApplyMPData getFVehicleInOutData(String driverId,String state) {
		// TODO Auto-generated method stub
		FInOutApplyMPData fInOutApplyMPData;
		try{
			String hql="select new com.ssh.model.pdata.FInOutApplyMPData(f,v,fio) " +
					"from Vehicle as v,FcarDriver as f,FcarAplly as fc,FcarInout as fio " +
					" where v.vehicleId=f.carId  and fc.fcarCarid=f.carId and f.driverId=:driverId " +
					"and fc.applyResult=:applyResult and fio.fcarCardId=f.fcarcardId and fio.state=:state";
		fInOutApplyMPData = (FInOutApplyMPData) sessionFactory.getCurrentSession().
					createQuery(hql).setParameter("driverId", driverId).
					setParameter("state", state).setInteger("applyResult", 2).uniqueResult();
		}
		catch(Exception e){
			e.printStackTrace();
			return null;
		}
		return fInOutApplyMPData;
	}

	@SuppressWarnings("unchecked")
	@Override
	public FcarAplly findByCidAndRid(int id) {
		String hql=" from FcarAplly as f where idfcarAplly="+id;
		List<FcarAplly> apps = sessionFactory.getCurrentSession().createQuery(hql).list();
		if(apps==null || apps.size()==0) return null;
		return apps.get(0);
	}

	@Override
	public void save(FcarAplly app) {
		// TODO Auto-generated method stub
		sessionFactory.getCurrentSession().merge(app);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<FInOutApplyMPData> getFInOutApplyByState(short state) {
		// TODO Auto-generated method stub
		Session s=sessionFactory.getCurrentSession();
		String hql = "select new com.ssh.model.pdata.FInOutApplyMPData(fa,fd,v) " +
				"from FcarDriver as fd,Vehicle as v,FcarAplly as fa " +
				"where (fa.fcarCarid=v.vehicleId)  and (fd.DLicence.dlicenceId=fa.DLicence.dlicenceId)" +
				" and fa.applyResult=:state";
		List<FInOutApplyMPData> fInOutApplyMPDataList = s.createQuery(hql).setParameter("state", state).list();
		return fInOutApplyMPDataList;
	}

}
